# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Maintainer: Joan Karadimov <joan.karadimov@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=pywin32
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
url='https://github.com/mhammond/pywin32'
pkgdesc='Python for Windows Extensions (mingw-w64)'
license=('spdx:PSF-2.0')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgver=306
pkgrel=2
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "binutils")
             "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://github.com/mhammond/${_realname}/archive/b${pkgver}.zip
        001-compile-and-setup-fixes.patch
        002-com-extensions-fix.patch
        003-win32-extensions-fix.patch
        004-isapi-fix.patch
        005-swig-fix.patch
        006-mapi-headers-fix.patch
        010-workaround-broken-add-dll-dir.patch)
sha256sums=('28b9186237cb097816072ffc9e55a1d7332c07aac07be18e43f7e12ef9691f0a'
            '8e0de5ab4ae0ecd88c70c4bfac28d9921f13979fb687ef676bfca21b95875030'
            '47e4cfa575a7988caca160beb328d76a1ae8f15cf1cdd0e472c7d2a4ba326ae8'
            'c03977d5a8bf43a06bf4390bb0bd63046361bd273ee08627f4a4bb86c2721492'
            '6b7045764e92d34c292aae3ddb01be60ece593384e30126eb7773386a709884c'
            '0ad3acc9c946a558a7d4c13574c6f4a25e897a9f81daff4b8a4b09b1cf019c50'
            'a04e4c8ee86606188c55349c4be755c72a9f03830d6f24828aba6bfa251e030d'
            '922cd17b6a0e1cde37e74611885de6e27977a20ae3eeb77cf0db1106d807a978')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
    cd "${srcdir}/${_realname}-b${pkgver}"
    apply_patch_with_msg \
    001-compile-and-setup-fixes.patch \
    002-com-extensions-fix.patch \
    003-win32-extensions-fix.patch \
    004-isapi-fix.patch \
    005-swig-fix.patch \
    006-mapi-headers-fix.patch

    # FIXME: https://github.com/msys2-contrib/cpython-mingw/issues/48
    patch -p1 < "${srcdir}"/010-workaround-broken-add-dll-dir.patch

    cd "${srcdir}"
    rm -rf "python-build-${MSYSTEM}" | true
    cp -r "${_realname}-b${pkgver}" "python-build-${MSYSTEM}"
}

build() {
    msg "Python build for ${MSYSTEM}"
    cd "${srcdir}/python-build-${MSYSTEM}"
    ${MINGW_PREFIX}/bin/python setup.py build --skip-verstamp
}

package() {
    cd "${srcdir}/python-build-${MSYSTEM}"
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
      --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 "win32/License.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.txt"
}
